
(function($){
	
	$(document).ready(function(){
		var counter = 1;
		var p_size = 1;
		var p_id = $("#p_id").val();

		$(".arrow.up")
			.addClass("clickable");
		$(".arrow.down")
			.addClass("clickable");

		// Decrease amount
		$(".arrow.up").on("click", function( e ){

			if(counter < 20) {
				// Keep track
				counter++;

				// Present
				var nval = counter * p_size;
				$("#product-quantity").html(nval);
			}
		});

		// Increase amount
		$(".arrow.down").on("click",function( e ){
			// Step down
			if(counter > 0){

				// Keep track
				counter--;

				// Present
				var nval = counter * p_size;
				$("#product-quantity").html(nval);
			}
		});

		// Clicking order
		$(".product-form").on("click", function( e ){
			// Keep track of button
			var el = $(e.target);
			var html = el.html();
			var data = {
				id : p_id,
				quantity : counter
			};

			// Working label...
			el.addClass("loading");
			
			// Add to cart via ajax
			$.ajax("/ajax/set_item/" + p_id + '/' + counter, 
			{
				type : "GET",
				success : function( data, xhr ){

					window.location.reload();
						
					if(data.status === true){

						// Reset button
						el.html(html);
						
						// Update cart
						$(window).trigger("cart_change");

					}
				},
				error : function( data, xhr ){
					console.error(data);
				},
				complete : function(){ el.removeClass("loading"); }
			});
		});
	});
})(jQuery);